<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程状态" prop="processStatus">
        <el-select
          v-model="queryParams.processStatus"
          placeholder="请选择流程状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.yy_flow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flow:FlowPatientInfo:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flow:FlowPatientInfo:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flow:FlowPatientInfo:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flow:FlowPatientInfo:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="FlowPatientInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_user_sex"
            :value="scope.row.sex"
          />
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="cardNo" />
      <el-table-column label="流程状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_flow_status"
            :value="scope.row.processStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="流程名称" align="center" prop="processName" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleStatus(scope.row)"
            v-hasPermi="['flow:FlowPatientInfo:edit']"
            >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            v-hasPermi="['flow:FlowPatientInfo:edit']"
            @click="handleCommand(scope.row)"
            v-if="scope.row.approvalIdentify!=1"
            >审批
          </el-button>
          <!-- <el-dropdown @command="handleCommand">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-thumb"
              v-hasPermi="['flow:FlowPatientInfo:edit']"
              >审批
            </el-button>
            <span class="el-dropdown-link">
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>通过</el-dropdown-item>
              <el-dropdown-item>拒绝</el-dropdown-item>
              <el-dropdown-item>终止</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flow:FlowPatientInfo:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flow:FlowPatientInfo:remove']"
            >删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改数据中台-患者药品申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="reply"
      width="700px"
      append-to-body
    >
      <el-card class="box-card" style="margin-bottom: 30px" shadow="never">
        <div slot="header" class="clearfix">
          <span>基本信息</span>
        </div>
        <el-descriptions v-if="userinfo">
          <el-descriptions-item label="姓名">{{
            userinfo.name
          }}</el-descriptions-item>
          <el-descriptions-item label="性别"
            ><dict-tag
              :options="dict.type.sys_user_sex"
              :value="userinfo.sex" /></el-descriptions-item
          ><el-descriptions-item label="年龄">{{
            userinfo.age
          }}</el-descriptions-item
          ><el-descriptions-item label="身份证号">{{
            userinfo.cardNo
          }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
      <el-card class="box-card" shadow="never" v-if="userinfo">
        <div slot="header" class="clearfix">
          <span>审批记录</span>
          <el-button
            style="float: right; padding: 3px 0"
            type="text"
            @click="handleCommand(userinfo)"
            v-if="userinfo.approvalIdentify!=1"
            >审批</el-button
          >
          <!-- <el-dropdown
            style="float: right; padding: 3px 0"
            @command="handleCommand"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-thumb"></i>审批<i
                class="el-icon-arrow-down el-icon--right"
              ></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a">通过</el-dropdown-item>
              <el-dropdown-item command="a">拒绝</el-dropdown-item>
              <el-dropdown-item command="a">终止</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown> -->
        </div>
        <el-timeline style="max-height: 300px; overflow-y: auto">
          <el-timeline-item
            v-for="(activity, index) in auditList"
            :key="index"
            :icon="activity.icon"
            :type="activity.type"
            :color="index == 0 ? '#13c2c2' : '#909399'"
            :size="index == 0 ? 'large' : 'normal'"
            :timestamp="activity.approvalTime"
          >
            <div style="display: flex; align-items: center">
              <dict-tag
                :options="dict.type.yy_audit_status"
                :value="activity.approvalStatus"
                style="margin-right: 10px"
              />{{ activity.processSubName }}
            </div>
            <p style="color: #5e6d82" v-if="activity.approvalName">审核人：{{ activity.approvalName }}</p>
            <p style="color: #5e6d82" v-if="activity.approvalRemake">
              审批备注：{{ activity.approvalRemake }}
            </p>
          </el-timeline-item>
        </el-timeline>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审批对话框 -->
    <el-dialog
      title="审批"
      :visible.sync="openAudit"
      width="500px"
      append-to-body
    >
      <el-form ref="auditForm" :model="auditForm" label-width="80px">
        <el-form-item label="审批" prop="approvalStatus">
          <el-radio-group v-model="auditForm.approvalStatus" size="medium">
            <el-radio
              v-for="(item, index) in approvalStatusOptions"
              :key="index"
              :label="item.value"
              :disabled="item.disabled"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="审批医生"
          prop="userId"
          v-if="auditForm.approvalStatus == 1"
        >
          <el-select
            v-model="auditForm.userId"
            filterable
            placeholder="请选择审批医生"
            clearable
            @change="changeValue($event)"
          >
            <el-option
              v-for="item in doctorList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审批备注" prop="approvalRemake">
          <el-input
            type="textarea"
            :rows="6"
            v-model="auditForm.approvalRemake"
            placeholder="请输入审批备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setAudit">确 定</el-button>
        <el-button @click="openAudit = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFlowPatientInfo,
  getFlowPatientInfo,
  delFlowPatientInfo,
  addFlowPatientInfo,
  updateFlowPatientInfo,
  auditApprovalRecord,
  listFlowApprovalRecord,
} from "@/api/flow/FlowPatientInfo";
import { listDoctor } from "@/api/dataCenter/doctor";
export default {
  name: "FlowPatientInfo",
  dicts: ["yy_flow_status", "sys_user_sex", "yy_audit_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者基本信息表格数据
      FlowPatientInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      reply: false, //查看
      openAudit: false, //审批弹窗
      approvalStatusOptions: [
        {
          label: "通过",
          value: 1,
        },
        {
          label: "驳回",
          value: 2,
        },
        {
          label: "终止",
          value: 3,
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        cardNo: null,
        processStatus: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      auditForm: {
        patientId: null,
        approvalStatus: 1,
        approvalRemake: null,
        userId: null,
        userName: null,
      },
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "患者姓名不能为空",
            trigger: "blur",
          },
        ],
        sex: [
          {
            required: true,
            message: "患者性别不能为空",
            trigger: "change",
          },
        ],
        age: [
          {
            required: true,
            message: "患者年龄不能为空",
            trigger: "blur",
          },
        ],
        cardNo: [
          {
            required: true,
            message: "患者身份证号不能为空",
            trigger: "blur",
          },
        ],
      },
      activities: [
        {
          content: "支持使用图标",
          timestamp: "2018-04-12 20:46",
          size: "large",
          type: "primary",
          icon: "el-icon-more",
        },
        {
          content: "支持自定义颜色",
          timestamp: "2018-04-03 20:46",
          color: "#0bbd87",
        },
        {
          content: "支持自定义尺寸",
          timestamp: "2018-04-03 20:46",
          size: "large",
        },
        {
          content: "默认样式的节点",
          timestamp: "2018-04-03 20:46",
        },
      ],
      auditList: [],
      userinfo: null, //用户基本信息
      doctorList: [],
    };
  },
  created() {
    this.getList();
    this.getlistDoctor();
  },
  methods: {
    changeValue(val) {
      console.log(val, "change value");
      if (!val) {
        this.auditForm.userName = null;
        return;
      }
      this.doctorList.forEach((item) => {
        if (val === item.userId) {
          this.auditForm.userName = item.userName;
        }
      });
    },
    /** 查询云HIS-医生信息列表 */
    getlistDoctor() {
      listDoctor({ pageNum: 1, pageSize: 1000 }).then((response) => {
        this.doctorList = response.rows;
      });
    },
    // 审批弹窗
    handleCommand(row) {
      this.auditForm.patientId = row.id;
      this.auditForm.approvalStatus = 1; //审批状态1=：通过，2:=驳回,3=：中止
      this.auditForm.approvalRemake = null;
      this.auditForm.userId = null;
      this.auditForm.userName = null;
      this.openAudit = true;
    },
    /** 查询患者基本信息列表 */
    getList() {
      this.loading = true;
      listFlowPatientInfo(this.queryParams).then((response) => {
        this.FlowPatientInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 审批操作 */
    setAudit() {
      if (this.auditForm.approvalStatus == 1 && !this.auditForm.userId) {
        this.$modal.msgError("请选择审批医生");
        return;
      }
      if (
        (this.auditForm.approvalStatus == 2 ||
          this.auditForm.approvalStatus == 3) &&
        !this.auditForm.approvalRemake
      ) {
        this.$modal.msgError("请填写审批备注");
        return;
      }
      this.$refs["auditForm"].validate((valid) => {
        if (valid) {
          auditApprovalRecord(this.auditForm).then((response) => {
            this.openAudit = false;
            this.reply = false;
            this.$modal.msgSuccess("审批成功");
            this.getList();
          });
        }
      });
    },
    // 查看
    handleStatus(row) {
      // this.reset();
      this.title = "查看";
      this.userinfo = row;
      this.reply = true;
      const id = row.id || this.ids;
      this.auditList = [];
      listFlowApprovalRecord({ patientId: id }).then((response) => {
        this.auditList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reply = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        age: null,
        cardNo: null,
        processId: null,
        processName: null,
        processSubId: null,
        processSubName: null,
        processStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加患者基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFlowPatientInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改患者基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFlowPatientInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.reply = false;
              this.getList();
            });
          } else {
            addFlowPatientInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该记录？")
        .then(function () {
          return delFlowPatientInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "flow/FlowPatientInfo/export",
        {
          ...this.queryParams,
        },
        `FlowPatientInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.el-dropdown-link {
  cursor: pointer;
  color: #13c2c2;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
